package com.triangle.cashier.adapter;

import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.triangle.cashier.R;
import com.triangle.cashier.bean.SelectedGoodsItem;

import java.util.List;

/**客户(副屏)显示适配器
 * Created by  on 2018/7/5.
 */

public class CustomerAdapter extends BaseAdapter {
    private LayoutInflater mInflater;//得到一个LayoutInfalter对象用来导入布局
    private Context context;
    private List<SelectedGoodsItem> list;

    public CustomerAdapter(Context context) {
        this.context = context;
        this.mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        if (list == null) {
            return 0;
        }
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {

            convertView = mInflater.inflate(R.layout.customer_item, null);
            holder = new ViewHolder();
            holder.nameTextView = convertView.findViewById(R.id.name_textView);
            holder.countTextView = convertView.findViewById(R.id.count_textView);
            holder.priceTextView = convertView.findViewById(R.id.price_textView);
            holder.totalTextView = convertView.findViewById(R.id.total_textView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        SelectedGoodsItem goodsItem = list.get(position);
        holder.nameTextView.setText(goodsItem.getName());
        holder.priceTextView.setText(goodsItem.getPrice() + "元");
        holder.countTextView.setText(goodsItem.getCount() + "");
        holder.totalTextView.setText(goodsItem.getTotal() + "元");
        if (position % 2 == 1) {
            convertView.setBackgroundColor(context.getColor(R.color.selected_goods_even_line_bg));
        } else {
            convertView.setBackgroundColor(context.getColor(R.color.selected_goods_odd_line_bg));
        }


        return convertView;
    }

    public void setData(List<SelectedGoodsItem> d) {
        list = d;
        notifyDataSetChanged();
    }


    /**
     * 存放控件
     */
    private final class ViewHolder {
        TextView nameTextView;
        TextView countTextView;
        TextView priceTextView;
        TextView totalTextView;

    }
}
